﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using System.Xml.Linq;
using System.Collections.Generic;

using YouHP.BLL;
using YouHP.Model;
using YouHP.Utility;

namespace web.admin
{
    public partial class ActivityNewsAdd : System.Web.UI.Page
    {
        private string URL_BACK = "ActivityNews.aspx";
        private ActivityNewsBLL activityNewsBll = ActivityNewsBLL.Instance;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void DataBound(object sender, EventArgs e)
        {
            //绑定是否可量化单选控件
            if (this.ViewState["hasImage"] == null || this.ViewState["hasImage"].ToString().Length <= 0)
            {
                this.ViewState["hasImage"] = "1";
            }
            ((RadioButtonList)this.activityNewsInfo.FindControl("hasImage")).SelectedValue = this.ViewState["hasImage"].ToString();
        }

        protected void InsertButton_click(object sender, EventArgs e)
        {
            try
            {
                ActivityNewsInfo info = validateForm(this.activityNewsInfo);

                if (info != null)
                {
                    if (activityNewsBll.AddActivityNews(info))
                    {
                        HttpContext.Current.Response.Redirect(URL_BACK, true);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void IsHasImageChanged(object sender, EventArgs e)
        {
            if (((RadioButtonList)this.activityNewsInfo.FindControl("hasImage")).SelectedValue.Equals("1"))
            {
                this.activityNewsInfo.Fields[3].Visible = true;
            }
            else
            {
                this.activityNewsInfo.Fields[3].Visible = false;
            }

            this.ViewState["hasImage"] = ((RadioButtonList)this.activityNewsInfo.FindControl("hasImage")).SelectedValue;
        }

        protected void CancelButton_click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect(URL_BACK, true);
        }

        private ActivityNewsInfo validateForm(DetailsView formViewCtl)
        {
            ActivityNewsInfo info = new ActivityNewsInfo();

            info.Title = ((TextBox)formViewCtl.FindControl("title")).Text;
            info.Keyword = ((TextBox)formViewCtl.FindControl("keyword")).Text;
            info.Introduction = ((TextBox)formViewCtl.FindControl("introduction")).Text;
            info.Body = ((TextBox)formViewCtl.FindControl("body")).Text;
            info.Area = ((TextBox)formViewCtl.FindControl("area")).Text;
            info.StartTime = Convert.ToDateTime(((TextBox)formViewCtl.FindControl("startTime")).Text);
            info.EndTime = Convert.ToDateTime(((TextBox)formViewCtl.FindControl("endTime")).Text);
            info.Dimensions = ((TextBox)formViewCtl.FindControl("dimensions")).Text;
            info.HasImage = Convert.ToInt32(((RadioButtonList)formViewCtl.FindControl("hasImage")).SelectedValue);
            info.UpdateTime = DateTime.Now;

            if (info.HasImage > 0)
            {
                info.ImagePath = ((TextBox)formViewCtl.FindControl("imagePath")).Text;
            }

            return info;
        }
    }
}

